<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/19
 * Time: 下午9:01
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class GroupController extends AdminBaseController{
    /**
     * 管理员分组列表页
     */
    public function index(){
        $this -> display();
    }

    /**
     * laypage 分组列表的  jquery.ajax 异步请求分页 数据处理
     * get到当前页的值$page  return 总页数 以及 循环出的列表内容
     */
    public function pageList(){
        $page = I('get.page/d',1);

        $group = M('group');

        // 查询满足要求的总记录数
        $count = $group -> count();

        $limit = 8;
        $pages = ceil($count / $limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);
        $list = $group -> order('id') -> limit($start.','.$limit) -> select();

        $content = '';
        foreach($list as $value){
            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['title'] . '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/group/rule/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 权限分配</a></td><td>';
            if($value['status']){
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-primary" id="status' . $value['id'] . '">正常</a>';
            }else{
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-danger" id="status' . $value['id'] . '">禁用</a>';
            }
            $content .= '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/group/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    /**
     * 添加用户组 表单
     */
    public function add(){
        $this -> display();
    }

    /**
     * 添加 修改 用户组
     */
    public function Save(){
        if(IS_POST && IS_AJAX){
            $id = I('post.id/d','');
            if(empty($id)){
                /**
                 * ID为空即为添加
                 */
                //  验证用户组名称
                $title = I('post.title/s','');
                if(!$title){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '用户组名称不能为空']);
                }
                $group = M('group');
                $r = $group ->field('id') -> where(['title' => $title]) -> find();
                if(!is_null($r)){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '用户组名称已被占用']);
                }

                $status = I('post.status/d',0);

                $data = [
                    'title' => $title,
                    'status' => $status
                ];
                $isAdd = $group -> add($data);
                if(!$isAdd){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '添加失败']);
                }
                $this -> ajaxReturn(['status' => 0, 'msg' => '添加成功']);
            }else{
                /**
                 * ID不为空即为修改
                 */
                //  验证用户组名称
                $title = I('post.title/s','');
                if(!$title){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '用户组名称不能为空']);
                }
                $group = M('group');
                //  通过提交的title 查询数据库
                $r = $group -> field('id') -> where(['title' => $title]) -> find();
                //  如果$r不为空则说明提交的title已存在
                if(!is_null($r)){
                    //  如果已存在的title 的id值 等于要修改的id值  说明查到的相同title是它本身 若不相等 说明名称已被占用
                    if($r['id'] != $id){
                        $this -> ajaxReturn(['status' => 1, 'msg' => '用户组名称已被占用']);
                    }
                }

                $status = I('post.status/d',0);

                $group = M('group');
                $isAdd = $group -> where(['id' => $id]) -> setField(['title' => $title, 'status' => $status]);
                if($isAdd === 0){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '别闹...你没有修改任何内容!']);
                }elseif($isAdd === false){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '修改失败']);
                }
                $this -> ajaxReturn(['status' => 0, 'msg' => '修改成功']);
            }
        }
    }

    /**
     * 修改用户组信息 表单
     */
    public function edit(){
        if(!IS_GET){
            $this -> error('非法操作','/Admin/group/index');
            exit;
        }
        $id = I('get.id/d',0);
        $group = M('group');
        $groupInfo = $group -> where(['id' => $id]) -> find();
        $this -> assign('arr',$groupInfo);
        $this -> display();
    }


    /**
     * 更改用户组 状态
     */
    public function statusChange(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            if(1 == $id){
                $this -> ajaxReturn(['status' => 1, 'msg' => '哥是超级管理员,无法禁用..']);
            }
            $group = M('group');
            $r = $group ->field('status') -> where(['id' => $id]) -> find();
            if(1 == $r['status']){
                $group -> where(['id' => $id]) -> setField('status', 0);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已禁用', 'data' => 0]);
            }else{
                $group -> where(['id' => $id]) -> setField('status', 1);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已开启', 'data' => 1]);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }
    /**
     * 删除用户组
     */
    public function del(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d');
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            if(1 == $id){
                $this -> ajaxReturn(['status' => 1, 'msg' => '哥是超级管理员,哥最叼..']);
            }
            $group = M('group');
            $isDel = $group -> delete($id);
            if(!$isDel){
                $this -> ajaxReturn(['status' => 0, 'msg' => '删除失败']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '删除成功']);
        }else{
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
        }
    }

    /**
     * 权限分配
     */
    public function rule(){
        if(IS_GET){
            /**
             * get形式获取ID   是表单页
             */
            $id = I('get.id/d',0);
            if(!$id){
                $this -> error('非法操作','/Admin/Group/index',1);
            }
            //  获取要分配 分组的权限信息
            $groupRule = M('group') -> field('id,rules,status') -> where(['id' => $id]) -> find();
            if(0 == $groupRule['status']){
                $this -> error('该用户组已被禁用','/Admin/Group/index',1);
            }
            //  获取权限列表
            $ruleInfo = M('Rule') ->field('id,pid,title') -> where(['status' => 1]) -> select();
            $ruleInfo = make_tree($ruleInfo);
            $rules = explode(',', $groupRule['rules']);
            $this -> assign('id', $id);
            $this -> assign('rules', $rules);
            $this -> assign('ruleInfo', $ruleInfo);
            $this -> display();
        }elseif(IS_POST && IS_AJAX){
            /**
             * post形式获取ID   是表单处理页
             */
            $id = I('post.id/d',0);
            $ruleIds = I('post.ruleIds/a',[]);
            $isUp = false;
            if($id && !empty($ruleIds)){
                $ruleIds = implode(',', $ruleIds);
                $isUp = M('Group')->where(['id' => $id])->setField(['rules' => $ruleIds]);
            }
            if($isUp){
                $this->ajaxReturn(['status' => 0 , 'msg' => '更新成功！']);
            }else{
                $this->ajaxReturn(['status' => 1 , 'msg' => '更新失败！']);
            }
        }else{
            $this -> error('非法操作','/Admin/Group/index',1);
        }
    }
}